<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="loginForm" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="loginForm.password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item
          prop="isAgree"
        >
          <el-checkbox v-model="loginForm.isAgree">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button class="login_btn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import router from '@/router'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: process.env.NODE_ENV === 'development' ? 'demo' : '',
        password: process.env.NODE_ENV === 'development' ? 'Hmzs%001' : '',
        isAgree: process.env.NODE_ENV === 'development'
      },
      rules: {
        username: [{
          required: true,
          message: '请输入账号信息',
          trigger: 'blur'
        },
        {
          pattern: /^[A-Za-z0-9]{4,8}$/,
          message: '用户名格式不正确',
          trigger: 'blur'

        }],
        password: [{
          required: true,
          message: '请输入密码信息',
          triggerL: 'blur'
        }, {
          min: 6,
          max: 16,
          message: '密码长度应该为6-16位之间',
          trigger: 'blur'

        }],
        isAgree: [{
          validator: (rule, value, callback) => {
            // rule规则
            // value检查的数据 true/false
            // callback 函数 执行这个函数
            // 成功执行callback 失败也执行callback(错误对象 new Error(错误信息))
            value ? callback() : callback(new Error('没有勾选用户平台协议'))
          }
        }]
      }

    }
  },
  methods: {
    login() {
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          await this.$store.dispatch('user/Sendlogin', {
            username: this.loginForm.username,
            password: this.loginForm.password
          })
          router.push('/')
        }
      })
    }
  }
}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
